import sys

from PyQt6 import QtCore
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSizePolicy


class MainWindow(QWidget):#继承QWidget
    def __init__(self, *args, **kwargs):#self is default parameter;you can add other parameters;
        #*args is mean any parameters; *kwargs is mean any dictronary parameters;
        super().__init__()#继承执行父类的构造函数
        self.setWindowTitle("登录验证") #set window title;be overrides the title
        #self.resize(400,300) #设置总体窗口大小,可以后期拉伸
        self.setFixedSize(400,300)#锁定大小,不可拉伸

        layout = QVBoxLayout() #创建一个垂直布局
        username_input=QHBoxLayout()#创建一个水平布局
        password_input=QHBoxLayout()
        button_input=QHBoxLayout()

        username_label=QLabel("username")
        username_edit=QLineEdit()
        #username_edit.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        username_input.addWidget(username_label,stretch=1)#把组件添加到布局中
        username_input.addWidget(username_edit,stretch=2)#它占父元素2/3
        password_label=QLabel("password")
        password_edit=QLineEdit()
        password_edit.setEchoMode(QLineEdit.EchoMode.Password)
        password_input.addWidget(password_label,stretch=1)
        password_input.addWidget(password_edit,stretch=2)
        login_button=QPushButton("login")
        login_button.setFixedWidth(200)

        #这是2中把元素居中的方式:
        #button_input.addWidget(login_button,alignment= QtCore.Qt.AlignmentFlag.AlignCenter)#设置按钮居中 水平位置 对齐方式
        button_input.addStretch(1)
        button_input.addWidget(login_button) #默认是0
        button_input.addStretch(1) #加了一个空白,占父元素1,于是把前面的按钮元素挤到左侧

        layout.addLayout(username_input)#把横布局添加到垂直布局中
        layout.addLayout(password_input)
        layout.addLayout(button_input)
        layout.setContentsMargins(50,20,50,20)#内边框

        self.setLayout(layout)#把布局添加到窗口中,否则不显示


        bg_color = kwargs.get('setbackgroundColor', None)
        if bg_color:
            self.setStyleSheet(f"background-color: {bg_color};")
        self.show()

if __name__ == "__main__":
    #print(sys.argv) #['D:\\u\\test\\gitee\\pythonqt\\test1\\改造程序\\new_hello2.py'] print sys.argv;default is this file path.
    app=QApplication(sys.argv)#get parameters from command line.
    w=MainWindow(setbackgroundColor="lightblue") #use MainWindow created a window.
    w.setWindowTitle("登录验证")



    w.show()#显示窗口
    #app.exec()#运行程序

    sys.exit(app.exec()) #app.exec() is first stop,then out process

    #import time #test app end,but process is not stop.
    #for i in range(10):#genarate a dictionary
    #    print(i)
    #    time.sleep(1)
# app=QApplication([])
# window=QWidget(windowTitle="Hello World",)#创建一个窗口
# window.show()#显示窗口
# app.exec()#运行程序
